<?php
namespace App\Models\VoidsModels;
use App\Models\ParentModel;
use DB;
use App\Components\Tools;
/**
 * 用户监控model
 * @author puppet
 */
class Voids extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_dy_user_video';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_dy_user_video';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;
	
	
	//查询未审核视频(合并 查询所有的实际)
	function getPageData($params){
		// echo "<pre>";print_r($params);exit();
		$qiniuUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 5;
		$page = isset($params['page']) ? intval($params['page']) : 1;
		$offset = ($page - 1) * $size;
        $search_status=isset($params['search_status']) ? intval($params['search_status']) : 99;
		$search_user=isset($params['search_user']) ? $params['search_user'] : '';
		$search_title=isset($params['search_title']) ? $params['search_title'] : '';
		$search_talk=isset($params['search_talk']) ? $params['search_talk'] : '';
		
		$countSql="select count(a.id) as count from $this->table as a left join pt_partner as b on b.id=a.partner_id where 1=1 ";
		$sql="select a.*,b.user_name from $this->table as a left join pt_partner as b on b.id=a.partner_id where 1=1 ";
		
		if($search_user){
			$countSql.=" and b.user_name like '%{$search_user}%'";
			$sql.=" and b.user_name like '%{$search_user}%'";
		}
		
		if($search_title){
			$countSql.=" and a.title like '%{$search_title}%'";
			$sql.=" and a.title like '%{$search_title}%'";
		}
		
		if($search_talk){
			$countSql.=" and a.talk like '%{$search_talk}%'";
			$sql.=" and a.talk like '%{$search_talk}%'";
		}
        if($search_talk){
            $countSql.=" and a.talk like '%{$search_talk}%'";
            $sql.=" and a.talk like '%{$search_talk}%'";
        }
        if($search_status!=99){
            $countSql.=" and a.status = {$search_status}";
            $sql.=" and a.status = {$search_status}";
        }
		$sql.=" order by a.create_time desc limit $offset,$size";
		// echo $sql;exit;
		$count=DB::select($countSql);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$data=DB::select($sql);
		
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				++$i;
				$data[$k]['kid']=$i;
				$data[$k]['title']="<div style='width:150px;height:100px;overflow:auto;'>{$v['title']} <font style=\"color:#00b7ee;\">{$v['talk']}</font></div>";
                $data[$k]['remark']="<div style='width:100px;height:100px;overflow:auto;'>{$v['remark']}</div>";
                $status='';
                if($v['status']==1){
                    $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>抖音审核中</span>";
                }elseif($v['status']==2){
                    $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00a65a;border-radius:13px;'>已发布</span>";
                }elseif($v['status']==-1){
                    $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#dd4b39;border-radius:13px;'>未通过</span>";
                }elseif($v['status']==3){
                    $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>后台任务中</span>";
                }else{
                    $data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#d2d6de;border-radius:13px;'>未审核</span>";
                }
                if(!empty($v['operator']) && !empty($v['status'])){
                    $data[$k]['operator']= $v['operator'];
                }elseif($v['status']==0){
                    $data[$k]['operator']='暂未审核';
                }else{
                    $data[$k]['operator']='系统审核';
                }
				$data[$k]['cover']="<a href='{$qiniuUrl}/{$v['cover']}' target='_blank'><img src='{$qiniuUrl}/{$v['cover']}' style='width:80px;height:100px;'></a>";
				$data[$k]['void']="<a href='{$qiniuUrl}/{$v['share_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['cover']}' style='width:80px;height:100px;'></a>";
				if($v['status']==0){
                    $data[$k]['operation']='<button class="btn btn-info btn-sm" onclick="saveVoid('."'{$v['id']}','1'".')">通过</button>';
                    $data[$k]['operation'].="<br/><br/>";
                    $data[$k]['operation'].='<button class="btn btn-defult btn-sm" onclick="saveVoid('."'{$v['id']}','-1'".')">不通过</button>';
                }else{
                    $data[$k]['operation']='<button class="btn btn-danger btn-sm" onclick="saveVoid('."'{$v['id']}','1'".')" disabled>通过</button>';
                    $data[$k]['operation'].="<br/><br/>";
                    $data[$k]['operation'].='<button class="btn btn-defult btn-sm" onclick="saveVoid('."'{$v['id']}','-1'".')" disabled>不通过</button>';
                }

				
				if($v['user_type']==1){
					$nickname=DB::select("select nickname from pt_user where open_id=? order by status desc limit 1",[$v['open_id']]);
				}elseif($v['user_type']==2){
					$nickname=DB::select("select tl_uname as nickname from pt_talent where open_id=? and tl_auth=1 order by tl_status desc limit 1",[$v['open_id']]);
				}
				$data[$k]['nickname']=isset($nickname[0]['nickname']) ? $nickname[0]['nickname'] : '';
			}
		}
		return ['data'=>$data,'total'=>$count];
	}
	
	//修改状态
	function updateAll($kid,$data){
		$sql="update $this->table set ";
		if(is_array($data)){
			foreach($data as $k=>$v){
				$sql.="`{$k}`='{$v}',";
			}
			$sql=substr($sql,0,-1)." where id={$kid}";
		}
		// echo $sql;exit;
		return DB::update($sql);
	}
	
	//查询视频信息
	function selectVideo($kid){
		$re=DB::select("select * from {$this->table} where id = ?",[$kid]);
		$re=isset($re[0]['id']) ? $re[0]: false;
		return $re;
	}
	
	//查询用户授权信息
	function selectToken($open_id){
		$re=DB::select("select * from pt_dy_access_token where open_id = ? and status = 1",[$open_id]);
		$re=isset($re[0]['id']) ? $re[0] : false;
		return $re;
	}
	
	//视频列表
	function getPageDataAll($params){
		$qiniuUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 5;
		$page = isset($params['page']) ? intval($params['page']) : 1;
		$offset = ($page-1) * $size;
		
		$search_user=isset($params['search_user']) ? $params['search_user'] : '';
		$search_title=isset($params['search_title']) ? $params['search_title'] : '';
		$search_talk=isset($params['search_talk']) ? $params['search_talk'] : '';
		$search_status=isset($params['search_status']) ? intval($params['search_status']) : 99;
		
		$countSql="select count(a.id) as count from $this->table as a left join pt_partner as b on b.id=a.partner_id where 1 = ?";
		$sql="select a.*,b.user_name from $this->table as a left join pt_partner as b on b.id=a.partner_id where 1 = ?";
		
		if($search_user){
			$countSql.=" and b.user_name like '%{$search_user}%'";
			$sql.=" and b.user_name like '%{$search_user}%'";
		}
		
		if($search_title){
			$countSql.=" and a.title like '%{$search_title}%'";
			$sql.=" and a.title like '%{$search_title}%'";
		}
		
		if($search_talk){
			$countSql.=" and a.talk like '%{$search_talk}%'";
			$sql.=" and a.talk like '%{$search_talk}%'";
		}
		
		if($search_status!=99){
			$countSql.=" and a.status = {$search_status}";
			$sql.=" and a.status = {$search_status}";
		}
		// echo $search_status;
		$sql.=" order by a.create_time desc limit $offset,$size";
		// echo $sql;exit;
		$count=DB::select($countSql,[1]);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$data=DB::select($sql,[1]);
		
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				++$i;
				$data[$k]['kid']=$i;
				$data[$k]['title']="<div style='width:150px;height:100px;overflow:auto;'>{$v['title']} <font style='color: #00b7ee'>{$v['talk']}</font></div>";

				if($v['status']==2){
					$data[$k]['cover']="<a href='{$v['cover']}' target='_blank'><img src='{$v['cover']}' style='width:80px;height:100px;'></a>";
					$data[$k]['void']="<a href='{$v['share_url']}' target='_blank'><img src='{$v['cover']}' style='width:80px;height:100px;'></a>";
				}else{
					$data[$k]['cover']="<a href='{$qiniuUrl}/{$v['cover']}' target='_blank'><img src='{$qiniuUrl}/{$v['cover']}' style='width:80px;height:100px;'></a>";
					$data[$k]['void']="<a href='{$qiniuUrl}/{$v['share_url']}' target='_blank'><img src='{$qiniuUrl}/{$v['cover']}' style='width:80px;height:100px;'></a>";
				}
				
				$data[$k]['remark']="<div style='width:100px;height:100px;overflow:auto;'>{$v['remark']}</div>";
				$status='';
				if($v['status']==1){
					$data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>抖音审核中</span>";
				}elseif($v['status']==2){
					$data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00a65a;border-radius:13px;'>已发布</span>";
				}elseif($v['status']==-1){
					$data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#dd4b39;border-radius:13px;'>未通过</span>";
				}elseif($v['status']==3){
					$data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#00c0ef;border-radius:13px;'>后台任务中</span>";
				}else{
					$data[$k]['status']="<span style='padding:5px 8px;color:#fff;border:1px solid transparent;background:#d2d6de;border-radius:13px;'>未审核</span>";
				}
				if(!empty($v['operator'])){
                    $data[$k]['operator']= $v['operator'];
                }else{
                    $data[$k]['operator']='系统审核';
                }
				if($v['user_type']==1){
					$nickname=DB::select("select nickname from pt_user where open_id=? order by status desc limit 1",[$v['open_id']]);
				}elseif($v['user_type']==2){
					$nickname=DB::select("select tl_uname as nickname from pt_talent where open_id=? and tl_auth=1 order by tl_status desc limit 1",[$v['open_id']]);
				}
				$data[$k]['nickname']=isset($nickname[0]['nickname']) ? $nickname[0]['nickname'] : '';
			}
		}
		return ['data'=>$data,'total'=>$count];
	}

}